/*
 * @Description: 数据转换
 * @Date: 2024-02-19 10:07:10
 * @FilePath: \demod:\12electron\electron-demo\src\renderer\src\utils\transData.ts
 */

// 将获取的串口Uint8Array数据转换成字符串
export const transUint8ArrayToString = (data: Uint8Array) => {
  let str = ''
  for (let i = 0; i < data.length; i++) {
    console.log(data[i])
    str += String.fromCharCode(data[i])
  }
  return str
}

// 将字符串下载为txt文件
export const downloadStrToTextFile = (str:string, fileName:string='文件') => {
  const blob = new Blob([str], {type: "text/plain;charset=utf-8"});
  const url = URL.createObjectURL(blob);

  const aTag = document.createElement('a');
  aTag.style.display = 'none';
  aTag.href = url;
  aTag.download = fileName+'.txt';

  document.body.appendChild(aTag);
  aTag.click();

  // 清理
  document.body.removeChild(aTag);
  URL.revokeObjectURL(url);
}
